home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / example / RoundButtonFilter.java < prev    next >
Encoding:
Text File  |  1997-07-13  |  5.6 KB  |  194 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)RoundButtonFilter.java    1.3 95/10/13  
  19.  *
  20.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  21.  *
  22.  * Permission to use, copy, modify, and distribute this software
  23.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  24.  * without fee is hereby granted. 
  25.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  26.  * for further important copyright and trademark information and to
  27.  * http://java.sun.com/licensing.html for further important licensing
  28.  * information for the Java (tm) Technology.
  29.  * 
  30.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  31.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  32.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  33.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  34.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  35.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  36.  * 
  37.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  38.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  39.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  40.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  41.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  42.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  43.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  44.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  45.  * HIGH RISK ACTIVITIES.
  46.  */
  47.  
  48. /**
  49.  * An extensible ImageMap applet class.
  50.  * The active areas on the image are controlled by ImageArea classes
  51.  * that can be dynamically loaded over the net.
  52.  *
  53.  * @author     Jim Graham
  54.  * @version     1.3, 13 Oct 1995
  55.  */
  56. class RoundButtonFilter extends ButtonFilter {
  57.     int Xcenter;
  58.     int Ycenter;
  59.     int Yradsq;
  60.     int innerW;
  61.     int innerH;
  62.     int Yrad2sq;
  63.  
  64.     public RoundButtonFilter(boolean press, int p, int b, int w, int h) {
  65.     super(press, p, b, w, h);
  66.     Xcenter = w/2;
  67.     Ycenter = h/2;
  68.     Yradsq = h * h / 4;
  69.     innerW = w - border * 2;
  70.     innerH = h - border * 2;
  71.     Yrad2sq = innerH * innerH / 4;
  72.     }
  73.  
  74.     public boolean inside(int x, int y) {
  75.     int yrel = Math.abs(Ycenter - y);
  76.     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
  77.     return (x >= Xcenter - xrel && x < Xcenter + xrel);
  78.     }
  79.  
  80.     public void buttonRanges(int y, int ranges[]) {
  81.     int yrel = Math.abs(Ycenter - y);
  82.     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
  83.     int xslash = width - (y * width / height);
  84.     ranges[0] = 0;
  85.     ranges[1] = Xcenter - xrel;
  86.     ranges[6] = Xcenter + xrel;
  87.     ranges[7] = width;
  88.     if (y < border) {
  89.         ranges[2] = ranges[3] = ranges[4] = Xcenter;
  90.         ranges[5] = ranges[6];
  91.     } else if (y + border >= height) {
  92.         ranges[2] = ranges[1];
  93.         ranges[3] = ranges[4] = ranges[5] = Xcenter;
  94.     } else {
  95.         int xrel2 = (int) (Math.sqrt(Yrad2sq - yrel * yrel)
  96.                    * innerW / innerH);
  97.         ranges[3] = Xcenter - xrel2;
  98.         ranges[4] = Xcenter + xrel2;
  99.         if (y < Ycenter) {
  100.         ranges[2] = ranges[3];
  101.         ranges[5] = ranges[6];
  102.         } else {
  103.         ranges[2] = ranges[1];
  104.         ranges[5] = ranges[4];
  105.         }
  106.     }
  107.     }
  108.  
  109.     private int savedranges[];
  110.     private int savedy;
  111.  
  112.     private synchronized int[] getRanges(int y) {
  113.     if (savedranges == null || savedy != y) {
  114.         if (savedranges == null) {
  115.         savedranges = new int[8];
  116.         }
  117.         buttonRanges(y, savedranges);
  118.         savedy = y;
  119.     }
  120.     return savedranges;
  121.     }
  122.  
  123.     public int filterRGB(int x, int y, int rgb) {
  124.     boolean brighter;
  125.     int percent;
  126.     int i;
  127.     int xrel, yrel;
  128.     int ranges[] = getRanges(y);
  129.     for (i = 0; i < 7; i++) {
  130.         if (x >= ranges[i] && x < ranges[i+1]) {
  131.         break;
  132.         }
  133.     }
  134.     switch (i) {
  135.     default:
  136.     case 0:
  137.     case 6:
  138.         return rgb & 0x00ffffff;
  139.     case 1:
  140.         brighter = !pressed;
  141.         percent = defpercent;
  142.         break;
  143.     case 5:
  144.         brighter = pressed;
  145.         percent = defpercent;
  146.         break;
  147.     case 2:
  148.         yrel = y - Ycenter;
  149.         xrel = Xcenter - x;
  150.         percent = (int) (yrel * defpercent * 2 /
  151.                  Math.sqrt(yrel * yrel + xrel * xrel))
  152.         - defpercent;
  153.         if (!pressed) {
  154.         percent = -percent;
  155.         }
  156.         if (percent == 0) {
  157.         return rgb;
  158.         } else if (percent < 0) {
  159.         percent = -percent;
  160.         brighter = false;
  161.         } else {
  162.         brighter = true;
  163.         }
  164.         break;
  165.     case 4:
  166.         yrel = Ycenter - y;
  167.         xrel = x - Xcenter;
  168.         percent = (int) (yrel * defpercent * 2 /
  169.                  Math.sqrt(yrel * yrel + xrel * xrel))
  170.         - defpercent;
  171.         if (pressed) {
  172.         percent = -percent;
  173.         }
  174.         if (percent == 0) {
  175.         return rgb;
  176.         } else if (percent < 0) {
  177.         percent = -percent;
  178.         brighter = false;
  179.         } else {
  180.         brighter = true;
  181.         }
  182.         break;
  183.     case 3:
  184.         if (!pressed) {
  185.         return rgb & 0x00ffffff;
  186.         }
  187.         brighter = false;
  188.         percent = defpercent;
  189.         break;
  190.     }
  191.     return filterRGB(rgb, brighter, percent);
  192.     }
  193. }
  194.